<template>
  <div class="login-wrap" @keydown.enter="submitForm(ruleFormRef)">
    <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" class="login-form">
      <el-form-item prop="username">
        <el-input v-model="ruleForm.username" type="username" autocomplete="off" clearable>
          <template #prefix>
            <el-icon><User /></el-icon>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input v-model="ruleForm.password" type="password" autocomplete="off" :show-password="true" clearable>
          <template #prefix>
            <el-icon><Unlock /></el-icon>
          </template>
        </el-input>
      </el-form-item>
    </el-form>
    <div class="row-between-center mb-24">
      <el-checkbox v-model="checkAll" @change="handleIsRememberMe">记住我</el-checkbox>
      <el-link type="primary" :underline="false">忘记密码？</el-link>
    </div>
    <el-button :loading="logining" class="login-btn" type="primary" @click="submitForm(ruleFormRef)">登录</el-button>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import type { FormInstance } from "element-plus";
import { useRouter } from "vue-router";
import { Calendar, Search, User, Unlock } from "@element-plus/icons-vue";
const router = useRouter();
const ruleFormRef = ref<FormInstance>();
// 表单校验规则
const validateUsername = (rule: any, value: any, callback: any) => {
  if (!value) {
    return callback(new Error("请输入账号"));
  }
  return callback();
};

const validatePassword = (rule: any, value: any, callback: any) => {
  if (value === "") {
    return callback(new Error("请输入密码"));
  }
  return callback();
};
// 表单对象
const ruleForm = reactive({
  username: "",
  password: "",
});
// 表单规则
const rules = reactive({
  username: [{ validator: validateUsername, trigger: "blur" }],
  password: [{ validator: validatePassword, trigger: "blur" }],
});
const logining = ref(false);
// 提交表单
const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.validate((valid) => {
    if (valid) {
      logining.value = true;
      console.log("submit!");
      setTimeout(() => {
        router.push("/");
      }, 2000);
    } else {
      console.log("error submit!");
      return false;
    }
  });
};

// 是否记住密码
const checkAll = ref(false);
function handleIsRememberMe(isRememberMe: boolean | number | string) {
  checkAll.value = !!isRememberMe;
}
</script>

<style scoped lang="scss">
// 项目行间距
@mixin xl-item-box {
  height: 38px;
  padding: 6.5px 11px;
  box-sizing: border-box;
}
// 屏幕宽度小于 600px 时
@media screen and(max-width:600px) {
  .login-wrap {
    padding: 15px;
  }
}
// 正常pc屏幕时
.login-wrap {
  max-width: 404px;
  position: relative;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  .row-between-center {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  //   登录表单样式穿透
  :deep(.login-form) {
    .el-form-item {
      margin-bottom: 24px;
    }
    .el-input__inner {
      @include xl-item-box;
    }
  }
  .login-btn {
    @include xl-item-box;
  }
  .login-btn {
    width: 100%;
    :deep(span) {
      display: inline-block;
      font-size: 16px;
      letter-spacing: 2px;
    }
  }
  .mb-24 {
    margin-bottom: 24px;
  }
}
</style>
